//
//  BSTopicCell.m
//  baisibudejie
//
//  Created by 陈博文 on 15/12/25.
//  Copyright © 2015年 陈博文. All rights reserved.
//

#import "BSTopicCell.h"
#import "BSTopic.h"
#import "UIImageView+Header.h"

#import "BSTopicMusicView.h"
#import "BSTopicPictureView.h"
#import "BSTopicVideoView.h"

@interface BSTopicCell ()

/** profileImageView*/
@property (nonatomic ,weak)IBOutlet UIImageView *profileImageView;
/** nameLabel*/
@property (nonatomic ,weak)IBOutlet UILabel *nameLabel;
/** createdAtLabel*/
@property (nonatomic ,weak)IBOutlet UILabel *createdAtLabel;
/** text_label*/
@property (nonatomic ,weak)IBOutlet UILabel *text_label;

/** dingButton*/
@property (nonatomic ,weak)IBOutlet UIButton *dingButton;
/** caiButton*/
@property (nonatomic ,weak)IBOutlet UIButton *caiButton;
/** repostButton*/
@property (nonatomic ,weak)IBOutlet UIButton *repostButton;
/** commentButton*/
@property (nonatomic ,weak)IBOutlet UIButton *commentButton;
/** topCmtView*/
@property (nonatomic ,weak)IBOutlet UIView *topCmtView;
/** topCmtLabel*/
@property (nonatomic ,weak)IBOutlet UILabel *topCmtLabel;

/** 图片 */
@property (nonatomic, weak) BSTopicPictureView *pictureView;

/** 声音 */
@property (nonatomic, weak) BSTopicMusicView *voiceView;

@end

@implementation BSTopicCell

#pragma mark - 懒加载
- (BSTopicPictureView *)pictureView
{
    if (!_pictureView) {
        BSTopicPictureView *pictureView = [BSTopicPictureView bs_viewFromXib];
        [self.contentView addSubview:pictureView];
        _pictureView = pictureView;
    }
    return _pictureView;
}

- (BSTopicVideoView *)videoView
{
    if (!_videoView) {
        BSTopicVideoView *videoView = [BSTopicVideoView bs_viewFromXib];
        [self.contentView addSubview:videoView];
        _videoView = videoView;
    }
    return _videoView;
}

- (BSTopicMusicView *)voiceView
{
    if (!_voiceView) {
        BSTopicMusicView *voiceView = [BSTopicMusicView bs_viewFromXib];
        [self.contentView addSubview:voiceView];
        _voiceView = voiceView;
    }
    return _voiceView;
}


- (void)awakeFromNib
{
    self.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"mainCellBackground"]];
}

// Xcode插件的安装路径
// 以前: /Users/用户名/Library/Application Support/Developer/Shared/Xcode/Plug-ins
// 现在: /Users/用户名/Library/Developer/Xcode/Plug-ins

- (void)setTopic:(BSTopic *)topic
{
    _topic = topic;
    
    // 弹出emoji表情键盘 : Control + Command + 空格
    
    // 设置头像
    [self.profileImageView bs_setHeader:topic.profile_image];
    
    self.nameLabel.text = topic.name;
    self.createdAtLabel.text = topic.created_at;
    self.text_label.text = topic.text;
    
    // 最热评论
    if (topic.top_cmt.count) { // 有最热评论
        self.topCmtView.hidden = NO;
        
        NSDictionary *cmt = topic.top_cmt.firstObject;
        NSString *username = cmt[@"user"][@"username"];
        NSString *content = cmt[@"content"];
        if (content.length == 0) { // 语音评论
            content = @"[语音评论]";
        }
        self.topCmtLabel.text = [NSString stringWithFormat:@"%@ : %@", username, content];
    } else { // 没有最热评论
        self.topCmtView.hidden = YES;
    }
    
    [self.dingButton setTitle:topic.ding forState:UIControlStateNormal];
    [self.caiButton setTitle:topic.cai forState:UIControlStateNormal];
    [self.repostButton setTitle:topic.repost forState:UIControlStateNormal];
    [self.commentButton setTitle:topic.comment forState:UIControlStateNormal];
   
    // 中间内容
    if (topic.type == BSTopicTypePicture) { // 图片
        //        self.pictureView.frame = topic.centerF;
        self.pictureView.hidden = NO;
        self.voiceView.hidden = YES;
        self.videoView.hidden = YES;
        self.pictureView.topic = topic;
    } else if (topic.type == BSTopicTypeMusic) { // 声音
        //        self.voiceView.frame = topic.centerF;
        self.pictureView.hidden = YES;
        self.voiceView.hidden = NO;
        self.voiceView.topic = topic;
        self.videoView.hidden = YES;
    } else if (topic.type == BSTopicTypeVideo) { // 视频
        //        self.videoView.frame = topic.centerF;
        self.pictureView.hidden = YES;
        self.voiceView.hidden = YES;
        self.videoView.hidden = NO;
        self.videoView.topic = topic;
    } else if (topic.type == BSTopicTypeWord) { // 段子
        self.pictureView.hidden = YES;
        self.voiceView.hidden = YES;
        self.videoView.hidden = YES;
    }

}

- (void)setFrame:(CGRect)frame
{
    //    frame.origin.x += BSMargin;
    //    frame.size.width -= 2 * BSMargin;
    
    [super setFrame:frame];
    frame.size.height -= BSMargin;
}
- (void)layoutSubviews
{
    [super layoutSubviews];
    
    if (self.topic.type == BSTopicTypePicture) { // 图片
        self.pictureView.frame = self.topic.centerF;
    } else if (self.topic.type == BSTopicTypeMusic) { // 声音
        self.voiceView.frame = self.topic.centerF;
    } else if (self.topic.type == BSTopicTypeVideo) { // 视频
        self.videoView.frame = self.topic.centerF;
    }
}



@end
